<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>表白墙</title>
</head>

<body>
    <div class="container">
        <h1>表白墙</h1>
        <p>输入后点击提交，会将信息显示至表白墙</p>
        <div class="row">
            <span>谁:</span>
            <input type="text" class="edits">
        </div>
        <div class="row">
            <span>对谁:</span>
            <input type="text" class="edits">
        </div>
        <div class="row">
            <span>想说:</span>
            <input type="text" class="edits">
        </div>
        <div class="row">
            <input type="button" id="submit" value="提 交">
        </div>
    </div>

    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        .container {
            width: 500px;
            height: 500px;
            background-color: rgba(233, 127, 180, 0.374);
            margin: 0 auto;
        }

        h1 {
            text-align: center;
            padding: 10px 0;
        }

        p {
            font-size: 14px;
            color: rgba(128, 128, 128, 0.407);
            text-align: center;
            padding: 10px 0;
        }

        .row {
            height: 50px;
            display: flex;
            justify-content: center;
            align-items: center;
        }

        span {
            width: 60px;
            font-size: 20px;
        }

        input {
            width: 200px;
            height: 40px;
        }

        #submit {
            width: 200px;
            color: white;
            background-color: orange;
            border: none;
            border-radius: 5px;
            font-size: 18px;
        }

        #submit:active {
            background-color: rgba(252, 214, 27, 0.877);
        }
    </style>

    <script src="https://lib.baomitu.com/jquery/3.6.0/jquery.min.js"></script>
    <script>
        //首先在页面加载的时候，从服务器上获取到内容
        function load() {
            $.ajax({
                type: 'GET',
                url: 'message',
                success: function (data, status) {
                    //let messages = JSON.parse(data);
                    let messages = data;
                    let container = document.querySelector('.container');
                    for (let message of messages) {
                        let row = document.createElement('div');
                        row.className = 'row';
                        row.innerHTML = message.from + '对' + message.to + '说' + message.words;
                        container.appendChild(row);
                    }
                }
            });
        }
        load();
        //点击提交的时候，把当前的数据构造一个HTTP请求，发送给服务器
        //1.捕获点击事件并处理
        let submit = document.querySelector('#submit');
        submit.onclick = function () {
            //2.先获取到编辑框的内容
            let edits = document.querySelectorAll('.edits');
            let from = edits[0].value;
            let to = edits[1].value;
            let words = edits[2].value;
            //3.进行简单的校验
            if (from == '' || to == '' || words == '') {
                alert("内容不能为空！");
                return;
            }
            //4.构造HTML元素,
            let row = document.createElement('div');
            row.className = 'row';
            row.innerText = from + "想对" + to + "说：" + words;
            //5.把新的元素插入到DOM树上
            let container = document.querySelector('.container');
            container.appendChild(row);
            //6.清空原来的输入框
            for (let i = 0; i < edits.length; i++) {
                edits[i].value = '';
            }
            //7.构造HTTP请求，把数据发送到服务器
            $.ajax({
                type: 'POST',
                url: 'message',
                data: JSON.stringify({ from: from, to: to, words: words }),
                contentType: 'application/json;charser=utf-8',
                success: function (data, status) {
                    if (data.ok == 1) {
                        console.log('提交成功');
                    } else {
                        console.log('提交失败');
                    }
                }
            })
        }

    </script>

</body>

</html>